<?php

require_once('jhtraq.php');
class JhtraqModelProjects extends JhtraqModelJhtraq
{
	
	function count()
	{
		$this->db->setQuery("Select count(*) as rows from #__jhtraq_projects");
		return (int)$this->db->loadResult();
	}
	
	function get($id)
	{
		$this->db->setQuery("Select * from #__jhtraq_projects where id='".(int)$id."'");
		return $this->db->loadObject();
	}
	
	function getSlug($slug)
	{
		$this->db->setQuery("Select * from #__jhtraq_projects where slug='$slug'");
		return $this->db->loadObject();
	}
	
	function getPaged($publishedOnly = false)
	{
		$mainframe =& JFactory::getApplication();
		$limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
		$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
		jimport('joomla.html.pagination');
		$search = JRequest::getVar('search');
		
		$query = "Select SQL_CALC_FOUND_ROWS * from #__jhtraq_projects";
		
		if($search != '')
		{
			$query .= " Where (name like'%$search%' or slug like '%$search%' or codename like '%$search%')";
			if($publishedOnly)
			$query .= " AND published = '1'";
		}else{
			if($publishedOnly)
			$query .= " Where published = '1'";
		}
		
		$query .= ' ORDER BY name ASC';
		
		$this->db->setQuery($query,$limitstart, $limit );
		$r =&$this->db->loadAssocList();
		//print_r($this->db);
		
		$this->db->setQuery('SELECT FOUND_ROWS();');
		$this->_pagination = new JPagination( $this->db->loadResult(), $limitstart, $limit );
		
		return $r;
	}
	
	function getList()
	{
		$this->db->setQuery("Select id,name from #__jhtraq_projects order by name asc");
		$res = $this->db->loadObjectList();
		$return=array();
		foreach($res as $row)
			$return[$row->id] = $row->name;
		
		return $return;
	}
	
	function save($post)
	{
		$id = (int)$post['id'];
		$name = $this->db->getEscaped($post['name']);
		$slug = $this->db->getEscaped($post['slug']);
		$codename = $this->db->getEscaped($post['codename']);
		$info = $this->db->getEscaped(stripslashes($post['info']));
		$displayorder = (int)$post['displayorder'];
		$managers = @implode(",",$post['managers']);
		$browse_svn = @implode(",",$post['browse_svn']);
		$svn_logs = @implode("," , $post['svn_logs']);
		$svn_url = $post['svn_url'];
		$svn_username = $post['svn_username'];
		$svn_password = $post['svn_password'];
		$add_tickets = implode(",", $post['add_tickets']);
                $amibros_tickets = implode(",", $post['amibros_tickets']);
		$comment_tickets = implode(",",$post['comment_tickets']);
		$edit_wiki = implode(",", $post['edit_wiki']);
		$published = (int)$post['published'];
		$tag = $post['tags'];
		$tag = trim($tag);
		$tag = html_entity_decode($tag, ENT_QUOTES);
		$tag = strtolower($tag);
		$tag = preg_replace('/\s{2,}/', ' ', $tag);
		$allowed = "/[^a-z0-9,\\040\\.\\-\\_\\\\]/i";
		$tag = preg_replace($allowed,"",$tag);
		$q = "Set name='$name', slug='$slug',codename='$codename',info='$info',displayorder='$displayorder',managers='$managers',browse_svn='$browse_svn',svn_logs='$svn_logs',svn_url='$svn_url',svn_username='$svn_username',svn_password='$svn_password',add_tickets='$add_tickets',comment_tickets='$comment_tickets',edit_wiki='$edit_wiki',tags='$tag',amibros_tickets='$amibros_tickets',published='$published'";
		if($id > 0)
		{
			//update
			$query = "Update #__jhtraq_projects " . $q . " where id='$id'";
			$this->db->setQuery($query);
			$res = $this->db->query();
// 			print_r($this->db);
// 			exit;
			return $res;
		}else{
			//insert
			$query = "Insert into #__jhtraq_projects " . $q;
			$this->db->setQuery($query);
			$this->db->query();
			$project_id = $this->db->insertid();
			//create our wiki page:::
			return $this->getModel('wiki')->createDefault($project_id);
		}
	}
	
	function remove($ids)
	{
		if(!is_array($ids))
			$ids = array();
		
		$this->db->setQuery("Delete from #__jhtraq_projects where id in(".implode(",",$ids) . ")");
		return $this->db->query();
	}
}
